package cn.tp.ksoft.civil_driver.module.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ViewAnimator;

import cn.tp.ksoft.civil_driver.DriverBaseActivity;
import cn.tp.ksoft.civil_driver.R;
import cn.tp.ksoft.civil_driver.bean.ResultBean;
import cn.tp.ksoft.civil_driver.request.DriverRequest;
import cn.tp.ksoft.civil_driver.request.RequestModules;
import cn.tp.ksoft.civil_driver.utils.Constants;
import cn.tp.ksoft.civil_driver.utils.Utils;
import cn.tp.ksoft.civil_driver.view.simple.SimpleTextWatcher;

/**
 * Created by zhusong on 16/5/1.
 */
public class ResetPwdActivity extends DriverBaseActivity implements View.OnClickListener, Runnable {
    private final int INDEX_WAYS = 0;
    private final int INDEX_GET_CODE = 1;
    private final int INDEX_RESET_PWD = 2;
    private ViewAnimator mContentController;

    private boolean isValidatePhone;
    private String mPhone;
    private String mPwd;
    // get code part
    private EditText mAccountEdt;
    private EditText mCodeEdt;
    private Button mGetCodeBtn;
    private Button mNextBtn;
    private String phone;
    private String validateCode;
    private int mLeaveTime;

    // reset pwd part
    private EditText mPwdEdt;
    private EditText mPwdAgainEdt;

    private WeakHandler mHandler = new WeakHandler(this);
    private DriverRequest request;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.driver_activity_find_pwd);
        Intent intent = getIntent();
        if (null != intent) {
            isValidatePhone = intent.getBooleanExtra(Constants.KEY_IS_VALIDATE_PHONE, false);
            mPhone = intent.getStringExtra(Constants.KEY_PHONE);
            mPwd = intent.getStringExtra(Constants.KEY_PWD);
        }


        request = new DriverRequest(this, mHandler);
        mContentController = (ViewAnimator) findViewById(R.id.content_control);
        // ways part
        findViewById(R.id.btn_byPhone).setOnClickListener(this);
        findViewById(R.id.btn_byEmail).setOnClickListener(this);

        // get code part
        mAccountEdt = (EditText) findViewById(R.id.edt_account);
        mAccountEdt.addTextChangedListener(new SimpleTextWatcher(){
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 11) {
                    // validate
                    request.validatePhone(s.toString());
                }
            }
        });
        mCodeEdt = (EditText) findViewById(R.id.edt_code);
        mGetCodeBtn = (Button) findViewById(R.id.btn_get_code);
        mGetCodeBtn.setOnClickListener(this);
        mNextBtn = (Button) findViewById(R.id.btn_next);
        mNextBtn.setOnClickListener(this);

        // reset pwd part
        mPwdEdt = (EditText) findViewById(R.id.edt_pwd);
        mPwdAgainEdt = (EditText) findViewById(R.id.edt_pwd_again);
        findViewById(R.id.btn_finish).setOnClickListener(this);



        if (isValidatePhone) {
            updateTitle(getString(R.string.driver_reg_valid_phone));
            mNextBtn.setText(getString(R.string.driver_login_login));
        } else {
            updateTitle(getString(R.string.driver_login_find));
        }
        if (!TextUtils.isEmpty(mPhone)) {
            mAccountEdt.setText(mPhone);
        }


    }

    @Override
    public void onBackPressed() {
        int currentIndex = mContentController.getDisplayedChild();
        if (currentIndex > INDEX_WAYS) {
            showContent(currentIndex - 1);
            return;
        }
        super.onBackPressed();
    }


    /**
     * show content
     * @param contentIndex
     */
    private void showContent(int contentIndex){
        int currentIndex = mContentController.getDisplayedChild();
        // back
        if (currentIndex > contentIndex) {
            mContentController.setInAnimation(this, R.anim.slide_in_from_l_to_r);
            mContentController.setOutAnimation(this, R.anim.slide_out_from_l_to_r);
        }
        // forward
        else if (currentIndex < contentIndex) {
            mContentController.setInAnimation(this, R.anim.slide_in_from_r_to_l);
            mContentController.setOutAnimation(this, R.anim.slide_out_from_r_to_l);
        }
        // do nothing
        else {
            return;
        }

        currentIndex = contentIndex;
        mContentController.setDisplayedChild(currentIndex);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_byPhone : {
                showContent(INDEX_GET_CODE);
                break;
            }
            case R.id.btn_byEmail : {
                showContent(INDEX_GET_CODE);
                break;
            }
            case R.id.btn_get_code : {
                String account = mAccountEdt.getText().toString();
                if (TextUtils.isEmpty(account)) {
                    Toast.makeText(this, getString(R.string.driver_please_input_phone),
                            Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!Utils.isPhone(account)) {
                    Toast.makeText(this, getString(R.string.driver_tip_input_correct_phone_number),
                            Toast.LENGTH_SHORT).show();
                    return;
                }

                mLeaveTime = 60;
                request.getValidateCode(account, RequestModules.OP_RESET_PWD);
                mHandler.post(this);
                mGetCodeBtn.setEnabled(false);
                break;
            }
            case R.id.btn_next : {
                String phone = mAccountEdt.getText().toString();
                String code = mCodeEdt.getText().toString();
                if (TextUtils.isEmpty(phone)
                        || TextUtils.isEmpty(code)) {
                    Toast.makeText(this, getString(R.string.driver_tip_input_phone_code),
                            Toast.LENGTH_SHORT).show();
                    return;
                }
                this.phone = phone;
                validateCode = code;

                if (isValidatePhone) {
                    request.login(phone, mPwd, validateCode);
                    return;
                }
                showContent(INDEX_RESET_PWD);
                break;
            }
            case R.id.btn_finish : {
                String pwd = mPwdEdt.getText().toString();
                String pwdAgain = mPwdAgainEdt.getText().toString();
                if (TextUtils.isEmpty(pwd)
                        || TextUtils.isEmpty(pwdAgain)) {
                    Toast.makeText(this, getString(R.string.driver_please_input_pwd),
                            Toast.LENGTH_SHORT).show();
                    return;
                }

                if (pwd.length() < 6 || pwdAgain.length() < 6) {
                    Toast.makeText(this, getString(R.string.driver_tip_pwd_min_len),
                            Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!pwd.equals(pwdAgain)) {
                    Toast.makeText(this, getString(R.string.driver_tip_pwd_not_equal),
                            Toast.LENGTH_SHORT).show();
                    return;
                }

                request.resetLoginPwd(phone, pwd, validateCode);
                break;
            }

        }
    }

    @Override
    public void run() {
        mLeaveTime--;
        if (mLeaveTime > 0) {
            String text = getString(R.string.driver_get_code_time, mLeaveTime);
            mGetCodeBtn.setText(text);
            mHandler.postDelayed(this, 1000);
        } else {
            String text = getString(R.string.driver_get_code);
            mGetCodeBtn.setText(text);
            mGetCodeBtn.setEnabled(true);
        }
    }

    @Override
    protected void handleSuccessResult(int what, @NonNull ResultBean result) {
        switch (what) {

            case RequestModules.VALIDATEPHONE : {
                mNextBtn.setEnabled(result.isSuccess());
                break;
            }

            case RequestModules.GETVALIDATECODE : {

                break;
            }

            case RequestModules.LOGIN : {
                setResult(RESULT_OK);
                finish();
                break;
            }

            case RequestModules.RESETLOGINPWD : {
                Toast.makeText(this, getString(R.string.driver_tip_reset_success), Toast.LENGTH_SHORT).show();
                finish();
                break;
            }
        }
    }
}
